package com.zhuchang.self_rpc.test.democoding.nettydemo;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @Author RenPu
 * @Date 2022/2/12 10:14
 * @Version 1.0
 * @Description:  Netty心跳检测机制实现，自定义业务逻辑的处理
 **/

public class MyServerHeartHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        //校验是否evt为IdleState的子类
        if(evt instanceof IdleStateEvent) {
            //进行类型转换
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;

            //进行状态值得逻辑判断，并做出响应
            String content = null;
            switch (idleStateEvent.state()) {
                case READER_IDLE:
                    content = "读空闲";
                    break;
                case WRITER_IDLE:
                    content = "写空闲";
                    break;
                case ALL_IDLE:
                    content = "读写空闲";
                    break;
            }
            System.out.println(ctx.channel().remoteAddress()+""+content);
            System.out.println("服务器做相应的处理.......");
        }

    }



}
